init("0", 2)
luaExitIfCall(true)

-- --------------初始化常量--------------------------
DEVICE_TYPE, DEVICE_ID, TS_VER, OS_VER = getDeviceType(), getDeviceID(), getTSVer(), getOSVer()
SCREEN_HEIGHT, SCREEN_WIDTH = getScreenSize()
COCAPP_IDENTIFIER = "com.supercell.magic"
-- warning
buttonWidth, buttonHeight = 0.0896 * SCREEN_WIDTH, 0.167 * SCREEN_HEIGHT
-- --------------初始化常量--------------------------

function ts_frontapp()
    front_app = frontAppBid();
    if front_app ~= COCAPP_IDENTIFIER then
        return false;
    else
        return true;
    end
end

function touchDU(x, y)
    if ts_frontapp() == false then
        return
    end


    touchDown(1, x, y)
    mSleep(50)
    touchUp(1, x, y)
    mSleep(50)
end

function cordFromTouch5(x, y)
    return SCREEN_WIDTH * x / 1136, SCREEN_HEIGHT * y / 640
end

function is_sameColor(r1, g1, b1, r2, g2, b2)
    return math.abs(r1 - r2) < 10 and math.abs(g1 - g2) < 10 and math.abs(b1 - b2) < 10
end


function getRealCord(key)
    if DEVICE_TYPE <= 1 then
        -- iTouch And iPhone
        local rawx, rawy, r, g, b = conf_16_9[key][1], conf_16_9[key][2], conf_16_9[key][3], conf_16_9[key][4], conf_16_9[key][5]
        local x, y = cordFromTouch5(rawx, rawy)
        return x, y, r, g, b
    else
        -- iPad
        return 0, 0, 0, 0, 0
    end
end


function is_okayColorForPointWithKey(key)
    local x, y, r, g, b = getRealCord(key)
    local realR, realG, realB = getColorRGB(x, y)
    return is_sameColor(r, g, b, realR, realG, realB)
end



-- -------------各种动作-----------------
-- 根据key来点击,跳过颜色检查
function touchButtonForKeySkipCheckClor(key)
    local x, y, r, g, b = getRealCord(key)
    touchDU(x, y)
end

-- 根据key来点击,并且检查颜色
function touchButtonForKey(key)
    local x, y, r, g, b = getRealCord(key)
    if is_okayColorForPointWithKey(key) then
        touchDU(x, y)
        return true
    end

    return false
end

-- 缩小地图small
function smallShrink()
    touchDU(10, 10)

    number = 5
    touchDown(1, SCREEN_WIDTH / number, SCREEN_HEIGHT / 2)
    touchDown(2, SCREEN_WIDTH * (number - 1) / number, SCREEN_HEIGHT / 2)
    mSleep(50)

    for i = 1, 400, 50 do
        touchMove(1, SCREEN_WIDTH / number + i, SCREEN_HEIGHT / 2)
        touchMove(2, SCREEN_WIDTH * (number - 1) / number - i, SCREEN_HEIGHT / 2)
        mSleep(50)
    end

    touchUp(1, SCREEN_WIDTH / number + 400, SCREEN_HEIGHT / 2)
    touchUp(2, SCREEN_WIDTH * (number - 1) / number - 400, SCREEN_HEIGHT / 2)

    mSleep(200)
end

-- 缩小地图
function shrink()
    smallShrink()
    smallShrink()
end


-- 地图上移
function moveUp()
    touchDown(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)
    mSleep(50)
    for i = 1, SCREEN_HEIGHT / 2, 50 do
        touchMove(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 + i)
        mSleep(50)
    end
    touchUp(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4)

    mSleep(200)
end



-- 地图下移
function moveDown()
    touchDown(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4)
    mSleep(50)
    for i = 1, SCREEN_HEIGHT / 2, 50 do
        touchMove(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 - i)
        mSleep(50)
    end
    touchUp(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)

    mSleep(200)
end

-- 回到主屏幕
function backToHome()
    if is_clanWarPage() then
        touchButtonForKeySkipCheckClor("returnHomeButton")
    end

    if is_donateOpen() then
        touchButtonForKey("donate_1", false)
    end

    touchButtonForKeySkipCheckClor("rightTopCorner", true)

    shrink()
    moveUp()
end


-- 出发干仗
function goBattle()
    backToHome()
    if touchButtonForKey("attackbutton") == false then
        return
    end
    if touchButtonForKey("findMatchButton") == false then
        return
    end
    touchButtonForKey("confirmGo")
end

-- 根据位置造兵
function trainDependsOnKey(key)
    for i = 1, 76, 1 do
        touchButtonForKey(key)
    end
end

-- 根据兵营的次序造兵
function trainDependsOnNumber(num)
    -- 1 弓箭, 2 胖子, 3 黄人, 4 胖子
    if num == 1 then
        trainDependsOnKey("trainAnchor")
    elseif num == 2 then
        trainDependsOnKey("trainGiant")
    elseif num == 3 then
        trainDependsOnKey("trainBarbarian")
    elseif num == 4 then
        trainDependsOnKey("trainGiant")
    end
end

-- 造兵
function trainSoldier()

    backToHome()

    touchButtonForKeySkipCheckClor("rightDownBarracks")

    if touchButtonForKey("trainSword") == false then
        return
    end

    local number = 0
    for i = 1, 8, 1 do
        if is_barracksTraining() then
            number = number + 1
            trainDependsOnNumber(number)
        end
        -- 切换
        if touchButtonForKey("barracks_1") == false then
            return
        end
    end
end

-- -------------各种动作-----------------

-- --------------sbAttack---------------

BUTTON_POSITIONS = {}
TROOPS_COLORS = {
    ["barbarian"] = { 0xF7AA21, 0xFFCF29, 0xFFB621, 0xFFE329 },
    ["archer"] = { 0xAD3452, 0xEF4573, 0x7B3C31, 0xFFD7C6 },
    ["goblin"] = { 0x63A6D6, 0xB5EB73, 0x438533, 0x3F6527 },
    ["gaint"] = { 0xFFBA8C, 0xFFC763, 0xEF9663, 0xF79663 },
    ["wallbreaker"] = { 0x5A5563, 0xBD5921, 0x312829, 0xFFE3CE },
    ["wizzard"] = { 0xEF9E94, 0x7B6552, 0xB56D63, 0xFFA294 },
    ["king"] = { 0xFFE384, 0xCE9A52, 0xD69694, 0xCE868C },
    ["queen"] = { 0x632CAD, 0xEFEFEF, 0x211429, 0x311810 },
    ["healspell"] = { 0xF7F3CE, 0xFFFBDE, 0xC67929, 0xFFE384 },
    ["ragespell"] = { 0xEFF3F7, 0xDECFF7, 0x422063, 0x6B20AD },
}

function getTroopParam(troopType, dx, dy)
    local troopColors = TROOPS_COLORS[troopType]
    local param = dx .. "|0|" .. string.format("0x%06X", troopColors[2]) .. ",0|" .. dy .. "|" .. string.format("0x%06X", troopColors[3]) .. "," .. dx .. "|" .. dy .. "|" .. string.format("0x%06X", troopColors[4])
    return string.format("0x%06X", troopColors[1]), param
end

function findTroopButtonKind(x1, y1, x2, y2)
    local dx, dy = math.floor(0.5 + buttonWidth / 4), math.floor(0.5 + buttonHeight / 4)
    for troopType, colors in pairs(TROOPS_COLORS) do
        local color, param = getTroopParam(troopType, dx, dy)
        local tmpX, tmpY = findMultiColorInRegionFuzzy(color, param, 80, x1, y1, x2, y2)
        if troopType == "queen" then

        end
        if tmpX ~= -1 and tmpY ~= -1 then
            return troopType
        end
    end
    return -1
end

function setTroopButtonsPosition()
    -- warning
    local startX, startY = 0.0083 * SCREEN_WIDTH, 0.7922 * SCREEN_HEIGHT
    for i = 1, 11 do
        x = startX + (i - 1) * buttonWidth
        local troopType = findTroopButtonKind(math.floor(0.5 + x), math.floor(0.5 + startY), math.floor(0.5 + x + buttonWidth), math.floor(0.5 + startY + buttonHeight))
        if troopType ~= -1 then
            BUTTON_POSITIONS[troopType] = { math.floor(0.5 + x + buttonWidth / 2), math.floor(0.5 + startY + buttonHeight / 2) }
        elseif BUTTON_POSITIONS["castle"] == nil then
            BUTTON_POSITIONS["castle"] = { math.floor(0.5 + x + buttonWidth / 2), math.floor(0.5 + startY + buttonHeight / 2) }
        end
    end
end

function setTroops(troopType, count, startX, startY, endX, endY)
    local dx, dy = (endX - startX) / (count - 1), (endY - startY) / (count - 1)
    local postion = BUTTON_POSITIONS[troopType]
    if postion == nil then
        return
    end
    local troopButtonX, troopButtonY = postion[1], postion[2]

    if troopButtonX > 0 and troopButtonY > 0 then
        touchDU(troopButtonX, troopButtonY)
        local x, y = -1, -1
        for i = 1, count do
            x = startX + dx * i
            y = startY + dy * i
            touchDU(x, y)
        end
    end
end

function setOneItem(itemType, x, y)
    local postion = BUTTON_POSITIONS[itemType]
    if postion == nil then
        return
    end
    local itemButtonX, itemButtonY = postion[1], postion[2]

    if itemButtonX > 0 and itemButtonY > 0 then
        touchDU(itemButtonX, itemButtonY)
        touchDU(x, y)
    end
end

function setItem(itemType, count, x, y)
    local postion = BUTTON_POSITIONS[itemType]
    if postion == nil then
        return
    end
    local itemButtonX, itemButtonY = postion[1], postion[2]

    if itemButtonX > 0 and itemButtonY > 0 then
        touchDU(itemButtonX, itemButtonY)
        for i = 1, count do
            touchDU(x, y)
        end
    end
end

function attackArea(area)
    local startX, startY, endX, endY, x, y = 0, 0, 0, 0, 0, 0
    if area == 0 then
        startX = 0.142 * SCREEN_WIDTH
        startY = 0.403 * SCREEN_HEIGHT
        endX = 0.391 * SCREEN_WIDTH
        endY = 0.145 * SCREEN_HEIGHT
    end

    setTroops("barbarian", 8, startX, startY, endX, endY)
    setTroops("archer", 12, startX, startY, endX, endY)
    setTroops("gaint", 6, startX, startY, endX, endY)
    setTroops("wallbreaker", 4, startX, startY, endX, endY)
    setTroops("goblin", 6, startX, startY, endX, endY)

end

function setTroopsAt(area, troopType, count)
    local startX, startY, endX, endY, x, y = 0, 0, 0, 0, 0, 0
    if area == 0 then
        startX = 0.142 * SCREEN_WIDTH
        startY = 0.403 * SCREEN_HEIGHT
        endX = 0.391 * SCREEN_WIDTH
        endY = 0.145 * SCREEN_HEIGHT
    end
    setTroops(troopType, count, startX, startY, endX, endY)
end

function attack()
    shrink()
    moveUp()
    setItem("gaint", 15, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
    setTroopsAt(0, "barbarian", 10)
    mSleep(1000)
    setItem("wallbreaker", 2, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
    setTroopsAt(0, "archer", 15)
    setItem("wallbreaker", 2, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
    setOneItem("castle", 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
    setOneItem("king", 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
    setOneItem("queen", 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)

    setTroopsAt(0, "archer", 15)
    setTroopsAt(0, "barbarian", 10)

    local healspellPostion = BUTTON_POSITIONS["healspell"]
    local ragespellPostion = BUTTON_POSITIONS["ragespell"]
    if healspellPostion ~= nil then
        setOneItem("healspell", 0.357 * SCREEN_WIDTH, 0.364 * SCREEN_HEIGHT)
    elseif ragespellPostion ~= nil then
        setOneItem("ragespell", 0.357 * SCREEN_WIDTH, 0.364 * SCREEN_HEIGHT)
    end

    setTroopsAt(0, "goblin", 10)

    for i=1,5 do
        setItem("barbarian", 5, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
        mSleep(500)
        setItem("archer", 5, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
        mSleep(500)
        setItem("goblin", 5, 0.276 * SCREEN_WIDTH, 0.266 * SCREEN_HEIGHT)
        mSleep(500)
    end

    setOneItem("king", -1, -1)
    setOneItem("queen", -1, -1)

    for i=1,6 do
        setTroopsAt(0, "barbarian", 10)
        setTroopsAt(0, "archer", 10)
        setTroopsAt(0, "goblin", 10)
    end
end

function sbAttack()
    setTroopButtonsPosition()
    attack()
end

-- --------------sbAttack---------------

initLog("test", 0)
sbAttack()
initLog("test", 0)
